//
//  YZMRedPacketActViewController.m
//  YZMicroStock
//
//  Created by apple on 17/5/2.
//  Copyright © 2017年 cqjr. All rights reserved.
//

#import "YZMRedPacketActViewController.h"
#import "YZMRedPacketActCell.h"
#import "YZClient.h"
#import "YZMRedPacketModel.h"
#import "YZMHomeBannerModel.h"

static NSString *const sYZMRedPacketActivityCellID = @"YZMRedPacketActivityCellID";

@interface YZMRedPacketActViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *datasource;
@property (nonatomic, assign) NSInteger activityId;

@end

@implementation YZMRedPacketActViewController

-(NSMutableArray *)datasource{
    
    if (!_datasource) {
        
        _datasource = [NSMutableArray array];
    }
    
    return _datasource;
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        self.hidesBottomBarWhenPushed = YES;
        self.title = @"红包活动";
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    [self createObjects];
    [self fetchActivityList];
//    self.datasource = [NSMutableArray arrayWithObjects:@{@"imageName":@"icon_hot",@"titleName":@"新用户注册", @"detail":@"新用户注册可送10元现金红包", @"money":@"10",@"btnState":@(YES)},@{@"titleName":@"模拟交易", @"detail":@"完成5次模拟练习", @"money":@"50",@"btnState":@(YES)},@{@"titleName":@"首次充值", @"detail":@"任意金额充值成功", @"money":@"30",@"btnState":@(YES)},@{@"titleName":@"实盘交易", @"detail":@"完成首次实盘交易", @"money":@"50",@"btnState":@(YES)},@{@"imageName":@"icon_new",@"titleName":@"实名认证", @"detail":@"完成实名认证送红包", @"money":@"18"}, nil];
    // Do any additional setup after loading the view.
}

- (void)createObjects {
    
    WS(ws)
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerClass:[YZMRedPacketActCell class] forCellReuseIdentifier:sYZMRedPacketActivityCellID];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(ws.view);
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WS(ws)
    YZMRedPacketActCell *cell = [tableView dequeueReusableCellWithIdentifier:sYZMRedPacketActivityCellID forIndexPath:indexPath];
    
    YZMRedPacketModel *model = _datasource[indexPath.row];
    cell.model = model;
    
    YZMIndexCount indexCount;
    indexCount.index =indexPath.row + 1;
    indexCount.count = self.datasource.count;
    cell.indexCount = indexCount;
    if (indexPath.row<self.datasource.count-1) {
        
        cell.isLastestCell = NO;
    }else{
        
        cell.isLastestCell = YES;
    }
    cell.action = ^(){
      
        [ws moveToActivityRelatedPage:indexPath];
    };
  
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.datasource.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self moveToActivityRelatedPage:indexPath];
}

/**
 跳转相关活动页面
 
 @param indexPath
 */
- (void)moveToActivityRelatedPage:(NSIndexPath *)indexPath {
    
    
    YZMRedPacketModel *dic = _datasource[indexPath.row];
    
    if (dic.status == 0) {
        
        //跳转相应活动页面
        [YZMHomeBannerModel tradeParam:dic.pageName withDic:nil];
    } else if (dic.status == 1) {
        
        [self getRedPacket:dic];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}

#pragma mark netWorking
-(void)fetchActivityList{
    
    WS(ws)
    [YZClient Activity_activityListNextWithactivityId:self.activityId success:^(NSURLSessionDataTask *task, id responseObject) {
        
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            
            [ws.datasource removeAllObjects];
            [ws.datasource addObjectsFromArray:topModel.data];
            [ws.tableView reloadData];
        }else{
            
            [topModel showMessage];
        }
        
    } failure:^(NSError *error) {
        
        [error yz_showMessage];
    }];
}

-(void)getRedPacket:(YZMRedPacketModel *)model{
    
    WS(ws)
    
    [YZClient Redpacket_fetchActivityListInfoWithTitle:model.title awardLogId:model.awardLogId money:model.value Success:^(NSURLSessionDataTask *task, id responseObject) {
        YZGeneralResultModel *topModel = responseObject;
        if (topModel.success) {
            
            [ws fetchActivityList];
            [MBProgressHUD showMessage:@"领取红包成功"];
        }else{
            
            [topModel showMessage];
        }
        
    } failure:^(NSError *error) {
        
        [error yz_showMessage];
    }];
}

#pragma mark - controller manager support

- (void)updateWithParams: (NSDictionary *)params{
    
    self.activityId = [params[@"activityid"] integerValue];
}

+ (instancetype)createWithParams: (NSDictionary *)params
{
    YZMRedPacketActViewController *ctrl = [[self alloc] init];
    [ctrl updateWithParams: params];
    return ctrl;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
